<?php
class UsersController extends AppController {
    public function beforeFilter() {
        $this->Auth->allow('login', 'register', 'view');
    }
    public function isAuthorized($user) {
        $this->loadModel('User');
        $this->User->id = $user['id'];
        $this->User->unbindModel(array('hasMany' => array('Test')));
        $role = $this->User->read('role');
        if($role['User']['role'] == 'none') {
            if(in_array($this->action, array('login', 'register', 'view', 'logout', 'edit', 'changepass'))) {
                return true;
            }
        }
        if($role['User']['role'] == 'admin') {
            if(in_array($this->action, array('login', 'register', 'view', 'logout', 'edit', 'changepass', 'admin_index'))) {
                return true;
            }
        }
    }
    /** Register **/
    public function register($step = null) {
        $this->set('title_for_layout', 'Đăng ký :: ChomoTech');
        if($step) {
            switch($step) {
                case 'step1':
                    $this->set('step', 'step1');
                    if($this->request->is('post')) {
                         
                        if($this->request->data['User']['agree'] == 1) {
                            $this->Session->write('step', 'step2');
                             
                            $this->redirect(array('controller' => 'users', 'action' => 'register', 'step2'));
                        } else {
                             
                            $this->redirect(array('controller' => 'users', 'action' => 'register', 'abort'));
                        }
                    }
                    break;
                case 'step2':
                    if($this->Session->read('step') == 'step2') {
                        $this->set('step', 'step2');
                        if($this->request->is('post')) {
                             
                            $this->request->data['User']['role'] = 'none';
                            $this->request->data['User']['like'] = '0';
                            $this->request->data['User']['dislike'] = '0';
                            $this->request->data['User']['lock'] = '0';
                            if($this->User->save($this->request->data['User'])) {
                                $this->Session->write('step', 'step3');
                                 
                                $this->redirect(array('controller' => 'users', 'action' => 'register', 'step3'));
                            }
                        }
                    } else {
                         
                        $this->redirect(array('controller' => 'users', 'action' => 'register', 'step1'));
                    }
                    break;
                case 'step3':
                    if($this->Session->read('step') == 'step3') {
                        $this->set('step', 'step3');
                    } else {
                         
                        $this->redirect(array('controller' => 'users', 'action' => 'register', 'step1'));
                    }
                    break;
                case 'abort':
                    $this->set('step', 'abort');
                    break;
                default:
                     
                    $this->redirect(array('controller' => 'users', 'action' => 'register', 'step1'));
                    break;
            }
        } else {
             
            $this->redirect(array('controller' => 'users', 'action' => 'register', 'step1'));
        }
    }
    /** Login **/
    public function login() {
        $this->Session->delete('step');
        $this->set('title_for_layout', 'Đăng Nhập :: ChomoTech');
        
        if($this->request->is('post')) {
             
            if($this->Auth->login()) {
                $this->User->id = $this->Auth->user('id');
                
                $this->User->unbindModel(array('hasMany' => array('Test')));
                
                $user = $this->User->read(array('id', 'username', 'role'));
                
                $this->Session->delete('Chomo.Current.User');
                $this->Session->delete('Chomo.Current.id');
                $this->Session->delete('Chomo.Current.Role');
                
                $this->Session->write('Chomo.Current.User', $user['User']['username']);
                $this->Session->write('Chomo.Current.id', $user['User']['id']);
                $this->Session->write('Chomo.Current.Role', $user['User']['role']);
                 
                $this->redirect($this->Auth->redirect());
            }
            else {
                $this->Session->setFlash('Tài khoản hoặc mật khẩu không dúng.');
            }
        } else if($this->Auth->loggedIn()) {
             
            $this->redirect(array('controller' => 'index'));
        }
    }
    /** Logout **/
    public function logout() {
        $this->Session->delete('Chomo.Current.User');
        $this->Session->delete('Chomo.Current.id');
        $this->Session->delete('Chomo.Current.Role');
        
        $this->Auth->logout();
        $this->redirect(array('controller' => 'index', 'action' => 'index'));
    }
    /** View **/
    public function view($id = null){
        $this->set('title_for_layout', 'Thông Tin Cá Nhân :: ChomoTech');
        
        if($id){
            $this->User->id = $id;
            if($this->User->exists()) {
                //Break relationship with Test model
                $this->User->unbindModel(array('hasMany' => array('Test')));

                $this->set('user', $this->User->read(array('id', 'username', 'email', 'phone', 'birthday')));
                $this->set('curr_user_id', $this->Auth->user('id'));
            } else {
                $this->Session->setFlash('Người dùng <b>'.$id.'</b> không tồn tại');
            }
        } else {
             
            $this->redirect(array('action' => 'view', $this->Auth->user('id')));
        }
    }
    /** Edit **/
    public function edit($id = null) {
        $this->set('title_for_layout', 'Sửa Thông Tin :: ChomoTech');
        
        if($id) {
            $this->User->id = $id;
            if(!$this->User->exists()) {
                 
                $this->redirect(array('controller' => 'index', 'action' => 'index'));
            }
            if($id != $this->Auth->user('id')) {
                 
                $this->redirect(array('controller' => 'index', 'action' => 'index'));
            }
            $this->User->unbindModel(array('hasMany' => array('Test')));
            $user = $this->User->read(array('email', 'phone', 'birthday'));
            $this->set('user', $user);
            if($this->request->is('post')) {
                 
                if($this->User->save($this->request->data, true, array('email', 'phone', 'birthday'))) {
                     
                    $this->redirect(array('controller' => 'users', 'action' => 'view'));
                }
            }
        } else {
             
            $this->redirect(array('action' => 'edit', $this->Auth->user('id')));
        }
    }
    /** Changepass **/
    public function changepass($id = null) {
        $this->set('title_for_layout', 'Đổi Mật Khẩu :: ChomoTech');
        
        if($id) {
            $this->User->id = $id;
            if($this->User->exists()) {
                if($this->User->id == $this->Auth->user('id')) {
                    if($this->request->is('post') && isset($this->request->data['User'])) {
                         
                        $this->User->unbindModel(array('hasMany' => array('Test')));
                        
                        $old_pass = $this->User->read(array('password'));
                        
                        if($this->Auth->password($this->request->data['Old']['old_password']) != $old_pass['User']['password']) {

                            $this->Session->setFlash('Mật khẩu cũ không đúng.');
                            
                        } else if($this->User->save($this->request->data, true, array('password'))) {
                            
                            $this->Session->setFlash('Đổi mật khẩu thành công.');
                             
                            $this->redirect(array('controller' => 'users', 'action' => 'view'));
                            
                        } else {
                            $this->Session->setFlash('Đổi mật khẩu không thành công.');
                        }
                    }
                } else {
                    $this->Session->setFlash('Bạn không có quyền sửa thông tin của người dùng <b>' . $id. '</b>.');
                     
                    $this->redirect(array('controller' => 'users', 'action' => 'view'));
                }
            } else {
                 
                $this->redirect(array('action' => 'changepass', $this->Auth->user('id')));  
            }
        } else {
             
            $this->redirect(array('action' => 'changepass', $this->Auth->user('id')));   
        }
    }
}
?>